<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
   //显示用户列表
    public function index(){
       
        //查询所有用户信息
        $users = DB::table('users')->paginate(4);

        return view('admin.user.index',['users'=>$users]);
    }

    //添加用户界面
    public function add(){
        return view('admin.user.add');
    }

    //添加用户操作
    public function insert(Request $request){
        //检测输入数据是否为空
        ///var_dump($request);
        if($request['uname']=='' || $request['upwd']=='' || $request['uemail']==''){
            return error('/admin/user/add','您填写的信息不完整',2);
        }
        
        //验证两次密码是否一致
        $pwd = $request->input('upwd');
        $repwd = $request->input('repwd');

        //var_dump($pwd);
        //var_dump($repwd);
        if($pwd != $repwd){
            return error('/admin/user/add','两次密码不一致',2);
        }

        //获取插入信息
        $user = $request->only(['uname','upwd','uemail']);
        //密码加密
        $user['upwd']=md5($user['upwd']);

        //将用户数据插入数据库
        $result = DB::table('users')->insert($user);

        //判断插入是否成功
        if($result){
            return success('/admin/user/index','插入用户成功',1);
        }else{
            return error('/admin/user/add','插入用户失败',1);
        }
    }

    //删除用户操作
    public function delete(Request $request){
        //接收用户ID
        $uid = $request->input('uid');
        //删除操作
        $result = DB::table('users')->where('uid','=',$uid)->delete();
        if($result){
            //删除成功  返回1
            return 1;
        }else{
            //删除失败  返回0
            return 0;
        }
    }

    //更新用户方法
    public function modify(Request $request){
        //获取用户ID
        $uid = $request->input('uid');
        //查询用户信息
        $user = DB::table('users')->where('uid','=',$uid)->first();

        //json编码返回
        return response()->json($user);

    }

    //更新用户操作
    public function update(Request $request){
        //检测是否需要修改密码
        if(!empty($request->input('upwd'))){
            //接收表单传参 更新数据到数据库中  修改密码
            //var_dump($request);
            $user = $request->only('uname','upwd','uemail');
            //判断2次密码是否一致
            if($user['upwd'] != $request->input('repwd')){
                return error('/admin/user/index','密码不一致',2);
            }
            //MD5加密
            $user['upwd'] = md5($user['upwd']);
        }else{
            //接收表单传参 更新数据到数据库中  不修改密码
            $user = $request->only('uname','uemail');
        }
        

        $uid = $request->input('uid');
        //var_dump($user);
        //var_dump($uid);
        

        $result = DB::table('users')->where('uid','=',$uid)->update($user);
        //var_dump($result);
        //判断执行结果
        if($result){
            return success('/admin/user/index','更新用户信息成功',1);
        }else{
            return error('/admin/user/index','更新用户信息失败',2);
        }
    }

    //查找用户界面
    public function find(){
        return view('admin.user.find');
    }

    //查询用户操作
    public function search(Request $request){
        //接收关键字
        $keywords = $request->input('key');

        $allUser = DB::table('users')
                ->where('uid','=',$keywords)
                ->orwhere('uname','like','%'.$keywords.'%')
                ->orwhere('uemail','like','%'.$keywords.'%')
                ->paginate(4);

        //将查找用户分配到模板中显示
        return view('admin.user.index',['users'=>$allUser]);
    }

}
